<!DOCTYPE html>
<html lang="zh">
<head>
  <title>全部订单</title>
  <{include file="public/header" /}>
  <style>
    .tihuo-address {
      text-align: left;
      padding: 5px;
      color: #2196f3;
      font-size: 12px;
      margin-left: -20px;
    }
  </style>
</head>
<body ontouchstart>
<header class="wy-header" style="position:fixed; top:0; left:0; right:0; z-index:200;">
  <div class="wy-header-icon-back"><span></span></div>
  <div class="wy-header-title">订单管理</div>
</header>
<div class='weui-content'>
  <div class="weui-tab">
    <div class="weui-navbar" style="position:fixed; top:44px; left:0; right:0; height:44px; background:#fff;">

      <a class="weui-navbar__item proinfo-tab-tit font-14 order-nav weui-bar__item--on" onclick="setStatus('')" href="#tab1">全部</a>
      <a class="weui-navbar__item proinfo-tab-tit font-14 order-nav0" onclick="setStatus(0)" href="#tab2">待付款</a>
      <a class="weui-navbar__item proinfo-tab-tit font-14 order-nav1" onclick="setStatus(1)" href="#tab3">待发货</a>
      <a class="weui-navbar__item proinfo-tab-tit font-14 order-nav2" onclick="setStatus(2)" href="#tab4">待收货</a>
      <a class="weui-navbar__item proinfo-tab-tit font-14 order-nav3" onclick="setStatus(3)" href="#tab5">待评价</a>
    </div>
    <div class="weui-tab__bd proinfo-tab-con orderlist" style="padding-top:87px;">
      <div id="tab1" class="weui-tab__bd-item order-list weui-tab__bd-item--active"></div>
      <div id="tab2" class="weui-tab__bd-item order-list0"></div>
      <div id="tab3" class="weui-tab__bd-item order-list1"></div>
      <div id="tab4" class="weui-tab__bd-item order-list2"></div>
      <div id="tab5" class="weui-tab__bd-item order-list3"></div>
    </div>
  </div>
</div>

<{include file="public/footer" /}>
<script>
  var page = 1;
  var status = '';
  var stop = false;
  let init_status = bs_get_param('status');
  $(document).ready(function () {
    if (init_status!==null){
      status = intval(init_status);
      $(".weui-navbar__item").removeClass('weui-bar__item--on');
      $(".order-nav"+init_status).addClass('weui-bar__item--on');
      $(".weui-tab__bd-item").removeClass('weui-tab__bd-item--active');
      $(".order-list"+init_status).addClass('weui-tab__bd-item--active');
      init_status = null;
    }
    // $("#tab1").empty()
    getData();
  })

  function getData(){
    if (stop){
      return ;
    }
    bs_request("<{:url('/mall/order/my_orders')}>",{page:page,status:status},function (res) {
      if (res.code==0 && res.data.orders.length>0){
        var idName = '';
        if (status==''){
          idName = 'tab1';
        }else if (status==0){
          idName = 'tab2';
        }else if (status==1){
          idName = 'tab3';
        }else if (status==2){
          idName = 'tab4';
        }else if (status==3){
          idName = 'tab5';
        }

        var orders = res.data.orders;
        for (var i=0;i<orders.length;i++){
          var item = '<div class="weui-panel weui-panel_access order-item-'+orders[i].order_sn+'" >\n' +
                  '          <div class="weui-panel__hd"><a href="<{:url(\'detail\')}>?order_sn='+orders[i].order_sn+'">单号：'+orders[i].order_sn+'</a><span class="ord-status-txt-ts fr">';



          if (orders[i].status=='0'){
            item+='待支付';
          }else if (orders[i].status=='1'){
            if (orders[i].order_type==2){
              item+='待提货';
            }else {
              item+='待发货';
            }
          }else if (orders[i].status=='2'){
            if (orders[i].order_type==3){
              item+='待确认';
            }else {
              item+='待收货';
            }
          }else if (orders[i].status=='3'){
            item+='待评价';
          }else if (orders[i].status=='4'){
            item+='已完成';
          }else if (orders[i].status=='-1'){
            item+='已取消';
          }else if (orders[i].status=='-2'){
            item+='已退款';
          }else {
            item+='待定';
          }

          item+='</span></div>\n' +
                  '          <div class="weui-media-box__bd  pd-10">\n' ;
          var proCount = 0;
          for(var j=0;j<orders[i].goods_list.length;j++){
            proCount++;

            item+= '            <div class="weui-media-box_appmsg ord-pro-list">\n' +
                    '              <div class="weui-media-box__hd"><a href="<{:url(\'/mall/goods/detail\')}>?id='+orders[i].goods_list[j].goods_id+'"><img class="weui-media-box__thumb" src="'+orders[i].goods_list[j].thumb+'" alt=""></a></div>\n' +
                    '              <div class="weui-media-box__bd">\n' +
                    '                <h1 class="weui-media-box__desc"><a href="<{:url(\'/mall/goods/detail\')}>?id='+orders[i].goods_list[j].goods_id+'" class="ord-pro-link">'+orders[i].goods_list[j].title+'</a></h1>\n' +
                    '                <!--<p class="weui-media-box__desc">规格：<span>红色</span>，<span>23</span></p>-->\n' +
                    '                <div class="clear mg-t-10">\n' +
                    '                  <div class="wy-pro-pri fl">¥<em class="num font-15">'+orders[i].goods_list[j].price+'</em></div>\n' +
                    '                  <div class="pro-amount fr"><span class="font-13">数量×<em class="name">'+orders[i].goods_list[j].count+'</em></span></div>\n' +
                    '                </div>\n' +
                    '              </div>\n' +
                    '            </div>\n';
          }


          item+='          <div class="ord-statistics">\n' +
                  '            <span>共<em class="num">'+proCount+'</em>件商品，</span>\n' +
                  '            <span class="wy-pro-pri">应付：¥<em class="num font-15">'+orders[i].pay_price+'</em></span>\n' +
                  '            <span>(含运费<b>￥0.00</b>)</span>\n' +
                  '          </div>\n'


          item+= ' <div class="weui-panel__ft">\n' +
                  ' <div class="weui-cell weui-cell_access weui-cell_link oder-opt-btnbox">\n'  ;

          if (orders[i].express_no){
            item+='<a href="<{:url(\'/mall/order/express\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com express" data-sn="'+orders[i].order_sn+'">查看物流</a>';
          }

          if (orders[i].status=='0'){
            item+='<a href="javascript:;" class="ords-btn-dele" data-sn="'+orders[i].order_sn+'">删除订单</a>' +
                    '<a href="<{:url(\'/mall/order/detail\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com" data-sn="'+orders[i].order_sn+'">去付款</a>';
          }else if(orders[i].status==1){

            if (orders[i].order_type==2){
              item+='<div class="tihuo-address" ><b>提货地址</b>：<span onclick="copy(this)">'+orders[i].tihuo_address+'</span></div>';
              item+='提货码：<span onclick="copy(this,false,1500)" class="tihuo-code">'+orders[i].code_tihuo+'</span>';
              item+='<a href="javascript:;" class="ords-btn-com receipt " data-sn="'+orders[i].order_sn+'">确认取货</a>';
            }else {
              item+='商品正在打包中，请您耐心等待....';
            }
            item+='<a href="javascript:;" class="ords-btn-com refund" data-sn="'+orders[i].order_sn+'">申请退款</a>';
          }else if(orders[i].status==2){
            if (orders[i].order_type==3){
              item+='<a href="<{:url(\'/mall/order/virtual\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com " data-sn="'+orders[i].order_sn+'">查看发货</a>';
              item+='<a href="javascript:;" class="ords-btn-com receipt" data-sn="'+orders[i].order_sn+'">确认收货</a>';
            }else {

              item+='<a href="javascript:;" class="ords-btn-com receipt" data-sn="'+orders[i].order_sn+'">确认收货</a>';

            }
          }else if(orders[i].status==3){
            if (orders[i].order_type==3){
              item+='<a href="<{:url(\'/mall/order/virtual\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com " data-sn="'+orders[i].order_sn+'">查看发货</a>';
            }
            item+=' <a href="<{:url(\'/mall/order/detail2\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com" data-sn="'+orders[i].order_sn+'">评价</a>';
          }else if (orders[i].status=='-1'){
            item+='<a href="javascript:;" class="ords-btn-dele" data-sn="'+orders[i].order_sn+'">删除订单</a>';
          }else{
            if (orders[i].order_type==3){
              item+='<a href="<{:url(\'/mall/order/virtual\')}>?order_sn='+orders[i].order_sn+'" class="ords-btn-com " data-sn="'+orders[i].order_sn+'">查看发货</a>';
            }
          }
          item+='</div> </div></div>';
          $("#"+idName).append(item)
        }
      }else{
        stop = true;
      }
    });
  }

  $(".orderlist").on('click','.refund',function () {
    var ordersn = $(this).data('sn')
    location.href = "<{:url('/mall/order/refund')}>?order_sn="+ordersn
    console.log(ordersn)
  });

  $(document).on("click", ".ords-btn-dele", function() {
    var order_sn = $(this).data('sn');
    $.confirm("您确定要删除此订单吗?", "确认删除?", function() {

      bs_request("<{:url('/mall/order/del')}>",{order_sn:order_sn},function (res) {

        $.toast(res.msg,res.code==0?'success':'cancel',1000);
        if (res.code==0){
          $(".order-item-"+order_sn).remove()
        }
      })
    }, function() {
      //取消操作
    });
  });

  $(document).on("click", ".receipt", function() {
    var order_sn = $(this).data('sn');
    $.confirm("您已经收到货了吗", "是否已经收到货?", function() {

      bs_request("<{:url('/mall/order/receive')}>",{order_sn:order_sn},function (res) {

        //$.toast(res.msg,res.code==0?'success':'cancel');
        if (res.code==0){
          $.alert("好东西记得五星好评哦，赶快去评价吧！", "收货完成！",function (res) {
            location.href = "<{:url('/mall/order/detail2')}>?order_sn="+order_sn
          });
        }else {
          $.toast(res.msg,'cancel');
        }
      })
    }, function() {
      //取消操作
    });

  });


  //改变状态

  function setStatus(nowStatus) {
    init_status = null;
    if (status == nowStatus){
      return ;
    }
    stop = false;
    status = nowStatus;
    page = 1;
    if (nowStatus==''){
      $("#tab1").empty()
    }else if (nowStatus=='0'){
      $("#tab2").empty()
    }else if (nowStatus==1){
      $("#tab3").empty()
    }else if (nowStatus==2){
      $("#tab4").empty()
    }else if (nowStatus==3){
      $("#tab5").empty()
    }
    getData();
  }

  $(window).scroll(function() {
    var scrollTop = $(this).scrollTop();
    var scrollHeight = $(document).height();
    var windowHeight = $(this).height();
    if (scrollTop + windowHeight == scrollHeight) {
      page++;
      getData();
    }
  });

</script>


</body>
</html>
